var selected="";
var uid=779340536;
var access_token;
var friendText="";
var oldtags = new Array();
var deletedtags = new Array();
var newtags = new Array();

function changeSelectionTo(naya)
{
	if(selected == naya)
		return;
	if(selected!="")
	{
		document.getElementById("nav_"+selected).setAttribute("class","non-selected");		
		//document.getElementById(selected).setAttribute("class","non-selected");
	}
	
	if(naya!="recent" && naya!="status")
	{
		document.getElementById("tabs-area").innerHTML = '';
	}
			
	selected=naya;
	
	if(selected!="")
	{
		document.getElementById("nav_"+naya).setAttribute("class","selected");	
	}
}

function asyncRequestObject()
{
	if (window.XMLHttpRequest)
	{
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return xmlhttp;
}

function profile(id,token,x,o,l)
{
	uid=id;
	access_token = token;
	//alert(o);
	if(isNaN(1*o))
		o=0;
	if(x=="recent"){
		recent(o,4);
	}
	else if(x=="toplinks"){
		toplinks(o,4);
	}
	else if(x=="groups"){
		groups(o,14);
	}
	else if(x=="friends"){
		friends(o,12);
	}
	else if(x=="tags"){
		tags();
	}
	else if(x=="status"){
		status();
	}
	else if(x=="invitations"){
		invitations(o,12);
	}
	else{
		recent(o,4);
	}
		
}

function status()
{
	
	document.getElementById("heading").innerHTML = "Status";
	document.getElementById('data').innerHTML= "<img src='images/wait.gif' />";
	xmlhttp = asyncRequestObject();

	var txt = '<ul id="tabs"><li id="status" class="selected"> <a onclick="javascript:status()">Status</a></li><li id="invitations" class="non-selected"> <a onclick="javascript:invitations(0,12)">Invitations</a></li> </ul>';
	document.getElementById("tabs-area").innerHTML = txt;
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('data').innerHTML=xmlhttp.responseText;
		}
	}

	xmlhttp.open("POST","status.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+uid);
	changeSelectionTo("status");
}

function recent(offset,limit)
{
	document.getElementById("heading").innerHTML = "Recent Links";
	document.getElementById('data').innerHTML= "<img src='images/wait.gif' />";
	xmlhttp = asyncRequestObject();
	
	var txt = '<ul id="tabs"><li id="recent" class="selected"> <a onclick="javascript:recent(0,4)">Recent</a></li><li id="toplinks" class="non-selected"> <a onclick="javascript:toplinks()">Top</a></li> </ul>';
	document.getElementById("tabs-area").innerHTML = txt;
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('data').innerHTML=xmlhttp.responseText;
		}
	}

	xmlhttp.open("POST","recent.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+uid+"&offset="+offset+"&limit="+limit);
	changeSelectionTo("recent");
}

function toplinks()
{
	document.getElementById('data').innerHTML= "<img src='images/wait.gif' />";
	document.getElementById("heading").innerHTML = "Top Links";
	xmlhttp = asyncRequestObject();
	
	var txt = '<ul id="tabs"><li id="recent" class="non-selected"> <a onclick="javascript:recent(0,4)">Recent</a></li><li id="toplinks" class="selected"> <a onclick="javascript:toplinks()">Top</a></li> </ul><br/>';
	document.getElementById("tabs-area").innerHTML = txt;
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('data').innerHTML=xmlhttp.responseText;
		}
	}

	xmlhttp.open("POST","toplinks.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+uid);
	changeSelectionTo("recent");
}

function tags()
{
	document.getElementById('data').innerHTML= "<img src='images/wait.gif' />";
	document.getElementById("heading").innerHTML = "My Tags";
	document.getElementById("tabs-area").innerHTML = "";
	xmlhttp = asyncRequestObject();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			xmlDoc = xmlhttp.responseXML;
			var txt="";
			x=xmlDoc.getElementsByTagName("LS-TAG");
			if(x.length==0)
			{
				txt = txt+"You currently do not have any links. To add tags, Add a link at the top and assign tags to that link";
			}
			else
			{
				var t,t2;
				txt+='<div class="tag_container">';
				for (i=0;i<x.length;i++)
				{
				  t = x[i].childNodes[0].nodeValue;
  				  //alert(t);
				  i++;
				  t2 = x[i].childNodes[0].nodeValue;
  				  //alert(t2);
				  txt=txt + '<a class="tag" onclick="javascript:singleTag(\''+t2+'\',0,4)" >'+t+'</a> ';
  				}
  				txt+='</div>';
			}
			document.getElementById('data').innerHTML=txt;
		}
	}

	xmlhttp.open("POST","tags.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+uid);
	changeSelectionTo("tags");
}

function groups(offset,limit)
{
	document.getElementById("heading").innerHTML = "My Groups";
	document.getElementById('data').innerHTML= "<img src='images/wait.gif' />";
	document.getElementById("tabs-area").innerHTML = "";
	xmlhttp = asyncRequestObject();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('data').innerHTML=xmlhttp.responseText;
		}
	}

	xmlhttp.open("POST","groups.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+uid+"&access_token="+access_token+"&offset="+offset+"&limit="+limit);
	changeSelectionTo("groups");
}

function friends(offset,limit)
{
	document.getElementById("heading").innerHTML = "My Friends";
	document.getElementById('data').innerHTML= "<img src='images/wait.gif' />";
	document.getElementById("tabs-area").innerHTML = "";
	
	/*
	var txt;
	FB.api(
	  {
		method: 'fql.query',
		query: 'SELECT uid,name FROM user WHERE is_app_user="true" and uid in (select uid2 from friend where uid1='+uid+')'
	  },
	  function(response) {
		if(response.length==0)
		{
			txt = "You currently do not have any links. To add tags, Add a link at the top and assign tags to that link";
		}
		else
		{
			txt='';
			for (i=0;i<response.length;i++)
			{
			  txt=txt + '<div class="friend-block"> <img class="pic" src="https://graph.facebook.com/'+response[i].uid+'/picture" /> <a class="normal" onclick="friendProfile('+response[i].uid+')">'+response[i].name+'</a></div>';
			}
		}
		document.getElementById('data').innerHTML = txt;
	  }
	);
	*/
	xmlhttp = asyncRequestObject();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			friendText = xmlhttp.responseText;
			document.getElementById('data').innerHTML=friendText;
		}
	}

	xmlhttp.open("POST","friends.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+uid+"&access_token="+access_token+"&offset="+offset+"&limit="+limit);
	changeSelectionTo("friends");
}

function invitations(offset,limit)
{
	document.getElementById('data').innerHTML= "<img src='images/wait.gif' />";
	document.getElementById("heading").innerHTML = "Invitations";
	
	var txt = '<ul id="tabs"><li id="status" class="non-selected"> <a onclick="javascript:status()">Status</a></li><li id="invitations" class="selected"> <a onclick="javascript:invitations(0,12)">Invitations</a></li> </ul>';
	document.getElementById("tabs-area").innerHTML = txt;
	xmlhttp = asyncRequestObject();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('data').innerHTML=xmlhttp.responseText;
			FB.XFBML.parse(document.getElementById('data'));
//			alert(xmlhttp.responseText);
		}
	}

	xmlhttp.open("POST","invitations.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+uid+"&access_token="+access_token+"&offset="+offset+"&limit="+limit);
	changeSelectionTo("status");
}

function addlink()
{
	//delete all the elements
	oldtags.splice(0,oldtags.length);
	newtags.splice(0,newtags.length);
	deletedtags.splice(0,deletedtags.length);
	
	
	
	document.getElementById('data').innerHTML= "<img src='images/wait.gif' />";
	document.getElementById("tabs-area").innerHTML = "";
	var link_url = document.getElementById('quickshare').value;
	document.getElementById('quickshare').value = 'Quickshare: Type or Paste URL';
	document.getElementById("heading").innerHTML = "Add a Link";
	xmlhttp = asyncRequestObject();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('data').innerHTML=xmlhttp.responseText;
		}
	}

	xmlhttp.open("POST","addlink.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+uid+"&access_token="+access_token+"&link_url="+link_url);
	changeSelectionTo("recent");
}

function alreadyExists(evnt)
{
  var existInOld=true;
  x=evnt.target.value;
  var idx = oldtags.indexOf(x);

  ab = document.getElementsByName(x);
  if(ab.length==0)
  {
    return false;
  }
  else if(oldtags.indexOf(x)!=-1 || newtags.indexOf(x)!=-1){
    return true;
  }
  else if(deletedtags.indexOf(x)!=-1){
    existInOld=false;
  }

  if(idx==-1)
  {
      if(existInOld)
		 toggleOldTag(x);
	  else
		 toggleNewTag(x);

	  ab[0].setAttribute('class','selected-oldtag');
	  	return true;
  }
  else{
    return true;
  }  
}
function toggleNewTag(x)
{
    var idx = newtags.indexOf(x);
	if(idx==-1){
		newtags.push(x);
		deletedtags.splice(deletedtags.indexOf(x),1);
	}
	else{
		newtags.splice(idx,1);
		deletedtags.push(x); 
	}
	//alert(newtags);
}

function toggleOldTag(x)
{
	if(oldtags[oldtags.indexOf(x)]==null){
		oldtags.push(x);
	}
	else{
		oldtags.splice(oldtags.indexOf(x),1);
	}
	//alert(oldtags);
}

function addTag(evnt)
{
  var x= evnt.target.value;
  if(evnt.keyCode == 13 && x != ''){
    if(alreadyExists(evnt))
    {
       evnt.target.value='';
       return;
    }
    
    var container = document.getElementById('newtag_container');
    var a = document.createElement("a");
    a.setAttribute('class','selected-newtag');
    a.setAttribute('name',x);
    a.setAttribute('onclick',"javascript:toggleNewTag(this.childNodes[0].nodeValue);if(getAttribute('class')=='newtag') {setAttribute('class','selected-newtag');} else  {setAttribute('class','newtag');}");
    a.innerHTML = x+' ';
    newtags.push(x);
    container.appendChild(a);
    evnt.target.value='';
  }
}

function utf8_encode ( argString ) {
    // Encodes an ISO-8859-1 string to UTF-8  
    // 
    // version: 1009.2513
    // discuss at: http://phpjs.org/functions/utf8_encode    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman    // +   improved by: Yves Sucaet
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Ulrich
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'    var string = (argString+''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n");
 
    var utftext = "";
    var start, end;
    var stringl = 0; 
    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);        var enc = null;
 
        if (c1 < 128) {
            end++;
        } else if (c1 > 127 && c1 < 2048) {            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc !== null) {            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;        }
    }
 
    if (end > start) {
        utftext += string.substring(start, string.length);    }
 
    return utftext;
}

function serialize (mixed_value) {
    // Returns a string representation of variable (which can later be unserialized)  
    // 
    // version: 1009.2513
    // discuss at: http://phpjs.org/functions/serialize    // +   original by: Arpad Ray (mailto:arpad@php.net)
    // +   improved by: Dino
    // +   bugfixed by: Andrej Pavlovic
    // +   bugfixed by: Garagoth
    // +      input by: DtTvB (http://dt.in.th/2008-09-16.string-length-in-bytes.html)    // +   bugfixed by: Russell Walker (http://www.nbill.co.uk/)
    // +   bugfixed by: Jamie Beck (http://www.terabit.ca/)
    // +      input by: Martin (http://www.erlenwiese.de/)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net/)
    // +   improved by: Le Torbi (http://www.letorbi.de/)    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net/)
    // -    depends on: utf8_encode
    // %          note: We feel the main purpose of this function should be to ease the transport of data between php & js
    // %          note: Aiming for PHP-compatibility, we have to translate objects to arrays
    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
    // *     example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
    // *     returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'
        var _utf8Size = function (str) {
            var size = 0, i = 0, l = str.length, code = '';            for (i = 0; i < l; i++) {
                code = str[i].charCodeAt(0);
                if (code < 0x0080) {
                    size += 1;
                } else if (code < 0x0800) {                    size += 2;
                } else {
                        size += 3;
                        }
            }            return size;
        };
    var _getType = function (inp) {
        var type = typeof inp, match;
        var key; 
        if (type === 'object' && !inp) {
            return 'null';
        }
        if (type === "object") {            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';    
    switch (type) {
        case "function": 
            val = ""; 
            break;        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;            break;
        case "string":
                        val = "s:" + _utf8Size(mixed_value) + ":\"" + mixed_value + "\"";
            break;
        case "array":        case "object":
            val = "a";
            /*
            if (type == "object") {
                var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);                if (objname == undefined) {
                    return;
                }
                objname[1] = this.serialize(objname[1]);
                val = "O" + objname[1].substring(1, objname[1].length - 1);            }
            */
            var count = 0;
            var vals = "";
            var okey;            var key;
            for (key in mixed_value) {
                            if (mixed_value.hasOwnProperty(key)) {
                   ktype = _getType(mixed_value[key]);
                       if (ktype === "function") {                            continue; 
                       }
               
                       okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                       vals += this.serialize(okey) +                               this.serialize(mixed_value[key]);
                       count++;
                        }
            }
            val += ":" + count + ":{" + vals + "}";            break;
        case "undefined": // Fall-through
        default: // if the JS object has a property which contains a null value, the string cannot be unserialized by PHP
            val = "N";
            break;    }
    if (type !== "object" && type !== "array") {
        val += ";";
    }
    return val;
}

function sharelink(evnt)
{
	//group id
	var grpid = document.getElementById("grp").value;
	var group_name;
	if(grpid==0)
	{
		//new group id for a new group name
		grpid = 1*document.getElementById("grp")[document.getElementById("grp").length-1].value+1;
		group_name=document.getElementById("text_new_group").value;
	}
	else
	{
		group_name=document.getElementById("grp").options[grpid].text;
	}
	//alert(group_name);
	
	var link_url = document.getElementById("link_url").innerHTML;
	var link_title = document.getElementById("link_title").innerHTML;
	// selected tags for the link - newtags, oldtags
	var serialize_newtags = serialize(newtags);
	var serialize_oldtags = serialize(oldtags);
	var link_desc = document.getElementById("linkdesc_text").innerHTML;
	var link_pic = document.getElementById("desc_pic").getAttribute('src');
	
	document.getElementById("heading").innerHTML = group_name;
	document.getElementById('data').innerHTML = "<img src='images/wait.gif' />";
	xmlhttp = asyncRequestObject();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('data').innerHTML = xmlhttp.responseText;
			FB.XFBML.parse(document.getElementById('data'));
		}
	}

	xmlhttp.open("POST","addlinktodb.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+uid+"&access_token="+access_token+"&grpid="+grpid+"&group_name="+group_name+"&link_url="+link_url+"&link_title="+link_title+"&serialize_newtags="+serialize_newtags+"&serialize_oldtags="+serialize_oldtags+"&link_desc="+link_desc+"&link_pic="+link_pic);
}

function group(id,xid,x,offset,limit)
{
	document.getElementById("heading").innerHTML = x;
	document.getElementById('data').innerHTML = "<img src='images/wait.gif' />";
	document.getElementById("tabs-area").innerHTML = "";
	xmlhttp = asyncRequestObject();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('data').innerHTML = xmlhttp.responseText;
		}
	}

	xmlhttp.open("POST","group.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+id+"&access_token="+access_token+"&group_name="+x+"&groupid="+xid+"&offset="+offset+"&limit="+limit);
	changeSelectionTo("groups");
}

function singleLink(id,linkid)
{
	document.getElementById("heading").innerHTML = "";
	document.getElementById('data').innerHTML = "<img src='images/wait.gif' />";
	document.getElementById("tabs-area").innerHTML = "";
	xmlhttp = asyncRequestObject();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('data').innerHTML = xmlhttp.responseText;
      		FB.XFBML.parse(document.getElementById('data'));
		}
	}

	xmlhttp.open("POST","singlelink.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+id+"&access_token="+access_token+"&linkid="+linkid);
}

function singleTag(x,offset,limit)
{
	xmlhttp = asyncRequestObject();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('data').innerHTML = xmlhttp.responseText;
		}
	}

	xmlhttp.open("POST","singleTag.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+uid+"&access_token="+access_token+"&tag="+x+"&offset="+offset+"&limit="+limit);	
	document.getElementById("heading").innerHTML = x;
	document.getElementById("tabs-area").innerHTML = "";
	document.getElementById('data').innerHTML = "<img src='images/wait.gif' />";
}

function inviteToApp()
{
	//sending into a php file to exclude friends already existing in the app
	xmlhttp = asyncRequestObject();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			//document.getElementById('data').innerHTML = xmlhttp.responseText;
			var data = document.getElementById("invite");
			document.getElementById("template").innerHTML = "";
			data.innerHTML=xmlhttp.responseText;
			FB.XFBML.parse(document.getElementById('data'));
		}
	}

	xmlhttp.open("POST","invitetoapp.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+uid+"&access_token="+access_token+"&page="+selected);	
	document.getElementById("tabs-area").innerHTML = "";
	document.getElementById('data').innerHTML = "<img src='images/wait.gif' />";
}

function invite(id,xid,x)
{
	//alert(id);
	//alert(xid);
	//alert(o);
	//sending into a php file to exclude friends already existing in the app
	xmlhttp = asyncRequestObject();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			//document.getElementById('data').innerHTML = xmlhttp.responseText;
			var data = document.getElementById("invite");
			document.getElementById("template").innerHTML = "";
			data.innerHTML=xmlhttp.responseText;
			FB.XFBML.parse(data);
		}
	}

	xmlhttp.open("POST","invite.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+id+"&access_token="+access_token+"&xid="+xid+"&x="+x);	
	document.getElementById("tabs-area").innerHTML = "";
	document.getElementById('data').innerHTML = "<img src='images/wait.gif' />";
}

function acceptInv(id,fid,xid,o)
{
	//sending into a php file to exclude friends already existing in the app
	xmlhttp = asyncRequestObject();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			if(xmlhttp.responseText=="true")
			{
				 node=document.getElementById(id+'-'+xid);
				 node.parentNode.removeChild(node);
				 invitations(o,12);
			}
			else
				alert(xmlhttp.responseText);
		}
	}

	xmlhttp.open("POST","accept.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+id+"&access_token="+access_token+"&xid="+xid+"&fid="+fid+"&o="+o);	
	//document.getElementById(id+'-'+xid).innerHTML = "<img src='images/wait.gif' />";
}

function cancelInv(id,fid,xid,o)
{
	//sending into a php file to exclude friends already existing in the app
	xmlhttp = asyncRequestObject();
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			if(xmlhttp.responseText=="true")
			{
				 node=document.getElementById(id+'-'+xid);
				 node.parentNode.removeChild(node);
				 invitations(o,12);
			}
			else
				alert(xmlhttp.responseText);
		}
	}

	xmlhttp.open("POST","cancel.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("uid="+id+"&access_token="+access_token+"&xid="+xid+"&fid="+fid+"&o="+o);	
	//document.getElementById(id+'-'+xid).innerHTML = "<img src='images/wait.gif' />";
}